package git.soulbgm.utils;

import io.netty.channel.ChannelHandlerContext;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * @author 贺瑞杰
 * @version V1.0
 * @date 2018-05-26 13:35
 * @description 对IP的工具类
 */
public class IpUtil {

    /***
     * 获取外网IP
     * @return
     */
    public static String internetIp() {
        try {
            Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
            InetAddress inetAddress = null;
            Enumeration<InetAddress> inetAddresses = null;
            while (networks.hasMoreElements()) {
                inetAddresses = networks.nextElement().getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    inetAddress = inetAddresses.nextElement();
                    if (inetAddress != null
                            && inetAddress instanceof Inet4Address
                            && !inetAddress.isSiteLocalAddress()
                            && !inetAddress.isLoopbackAddress()
                            && inetAddress.getHostAddress().indexOf(":") == -1) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
            return null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取内网IP
     *
     * @return
     */
    public static String intranetIp() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取服务启动host
     *
     * @return
     */
    public static String getHost() {
        return internetIp() == null ? intranetIp() : internetIp();
    }

    /**
     * 获取本地主机局域网地址 InetAddress
     *
     * @return
     */
    public static InetAddress getLocalHostLANAddress() {
        try {
            InetAddress candidateAddress = null;
            // 遍历所有的网络接口
            for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) {
                NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
                // 在所有的接口下再遍历IP
                for (Enumeration inetAddresses = iface.getInetAddresses(); inetAddresses.hasMoreElements(); ) {
                    InetAddress inetAddress = (InetAddress) inetAddresses.nextElement();
                    // 排除loopback类型地址
                    if (!inetAddress.isLoopbackAddress()) {
                        if (inetAddress.isSiteLocalAddress()) {
                            // 如果是site-local地址，就是它了
                            return inetAddress;
                        } else if (candidateAddress == null) {
                            // site-local类型的地址未被发现，先记录候选地址
                            candidateAddress = inetAddress;
                        }
                    }
                }
            }
            if (candidateAddress != null) {
                return candidateAddress;
            }
            // 如果没有发现 non-loopback地址.只能用最次选的方案
            InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
            return jdkSuppliedAddress;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取本地主机局域网地址
     *
     * @return
     */
    public static String getHostAddress() {
        return getLocalHostLANAddress().getHostAddress();
    }

    private static String localAddress = "0.0.0.0";

    /**
     * TCP方式
     * 通过ChannelHandlerContext获得IP
     *
     * @param ctx
     * @return
     */
    public static String getTcpHost(ChannelHandlerContext ctx) {
        String host = ctx.channel().remoteAddress().toString();
        host = host.substring(1, host.indexOf(":"));
        if (localAddress.equals(host)) {
            host = "127.0.0.1";
        }
        return host;
    }

    /**
     * UDP方式
     * 通过ChannelHandlerContext获得IP
     *
     * @param ctx
     * @return
     */
    public static String getUdpHost(ChannelHandlerContext ctx) {
//        Channel channel = ctx.channel().closeFuture().channel();
        String host = ctx.channel().localAddress().toString();
        host = host.substring(1, host.indexOf(":"));
        if (localAddress.equals(host)) {
            host = "127.0.0.1";
        }
        return host;
    }

}
